home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 22 / CU Amiga Magazine's Super CD-ROM 22 (1998)(EMAP Images)(GB)[!][issue 1998-05].iso / PowerPC / Programming / PPCSmallEiffel / lib_se / parser_buffer.e < prev    next >
Encoding:
Text File  |  1998-01-16  |  3.9 KB  |  177 lines

  1. --          This file is part of SmallEiffel The GNU Eiffel Compiler.
  2. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  3. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  4. --                       http://www.loria.fr/SmallEiffel
  5. -- SmallEiffel is  free  software;  you can  redistribute it and/or modify it 
  6. -- under the terms of the GNU General Public License as published by the Free
  7. -- Software  Foundation;  either  version  2, or (at your option)  any  later 
  8. -- version. SmallEiffel is distributed in the hope that it will be useful,but
  9. -- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. -- or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU General Public License 
  11. -- for  more  details.  You  should  have  received a copy of the GNU General 
  12. -- Public  License  along  with  SmallEiffel;  see the file COPYING.  If not,
  13. -- write to the  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. -- Boston, MA 02111-1307, USA.
  15. --
  16. class PARSER_BUFFER
  17.    
  18. inherit GLOBALS;
  19.    
  20. creation make
  21.    
  22. feature
  23.  
  24.    path: STRING;
  25.      -- When `is_ready', gives the `path' of the corresponding 
  26.      -- buffered file.
  27.    
  28.    count: INTEGER;
  29.      -- Number of lines in the source file.
  30.    
  31. feature {NONE}
  32.    
  33.    text: FIXED_ARRAY[STRING] is
  34.      -- To store the complete file to parse. Each line 
  35.      -- is one STRING without the '%N' end-of-line mark.
  36.       once
  37.      !!Result.with_capacity(2048);
  38.       end;
  39.  
  40. feature {NONE} -- Low level IO buffer :
  41.  
  42.    buffer_size: INTEGER is 4096;
  43.  
  44.    buffer: NATIVE_ARRAY[CHARACTER] is
  45.       once
  46.      Result := Result.calloc(buffer_size);
  47.       end;
  48.  
  49. feature {NONE}
  50.    
  51.    make is
  52.       do
  53.       end;
  54.  
  55. feature
  56.  
  57.    is_ready: BOOLEAN is
  58.       do
  59.      Result := path /= Void;
  60.       end;
  61.  
  62. feature {SMALL_EIFFEL,EIFFEL_PARSER}
  63.  
  64.    load_file(a_path: STRING) is
  65.      -- Try to load `a_path' and set `is_ready' when corresponding
  66.      -- file has been loaded.
  67.       do
  68.      count := read_file(a_path);
  69.      if count >= 0 then
  70.         path := a_path;
  71.      else
  72.         path := Void;
  73.      end;
  74.       end;
  75.  
  76.    unset_is_ready is
  77.       do
  78.      path := Void;
  79.       end;
  80.  
  81. feature {EIFFEL_PARSER}
  82.  
  83.    item(line: INTEGER): STRING is
  84.       require 
  85.      is_ready;
  86.      1 <= line;
  87.      line <= count
  88.       do
  89.      Result := text.item(line);
  90.       ensure
  91.      Result /= Void
  92.       end;
  93.    
  94. feature {NONE}
  95.    
  96.    read_file(a_path: STRING): INTEGER is
  97.      -- Result is -1 when `a_path' cannot be read.
  98.      -- Result gives the number of lines.
  99.       local
  100.      p: POINTER;
  101.      file, i, nb_read: INTEGER;
  102.      c: CHARACTER;
  103.      line: STRING;
  104.      b: like buffer;
  105.       do
  106.      p := a_path.to_external;
  107.      c_inline_c("_file=open(_p,O_RDONLY,0);");
  108.      if file >= 0 then
  109.         from
  110.            b := buffer; -- to speed up.
  111.            line := next_line(0); -- unused line.
  112.            line := next_line(1);
  113.            Result := 1;
  114.            nb_read := buffer_size;
  115.         until
  116.            nb_read < buffer_size
  117.         loop
  118.            nb_read := read(file,b,buffer_size);
  119.            from
  120.           i := 0;
  121.            until
  122.           i = nb_read
  123.            loop
  124.           c := b.item(i);
  125.           if c = '%N' then
  126.              Result := Result + 1;
  127.              line := next_line(Result);
  128.           elseif c = '%R' then
  129.           else
  130.              line.extend(c);
  131.           end;
  132.           i := i + 1;
  133.            end;
  134.         end;
  135.         if line.empty then
  136.            Result := Result - 1;
  137.         end;
  138.         file := close(file);
  139.      else
  140.         Result := -1;
  141.      end;
  142.       end;
  143.    
  144. feature {NONE}
  145.  
  146.    read(file: INTEGER; buf: like buffer; n: INTEGER): INTEGER is
  147.       external "C_InlineWithoutCurrent"
  148.       end;
  149.  
  150.    close(file: INTEGER): INTEGER is
  151.       external "C_InlineWithoutCurrent"
  152.       end;
  153.  
  154. feature {NONE}
  155.  
  156.    next_line(i: INTEGER): STRING is
  157.       require
  158.      i >= 0
  159.       do
  160.      if i <= text.upper then
  161.         Result := text.item(i);
  162.         Result.clear;
  163.      else
  164.         !!Result.make(medium_line_size);
  165.         text.add_last(Result);
  166.      end;
  167.       ensure
  168.      Result.empty;
  169.      Result.capacity >= medium_line_size;
  170.      text.item(i) = Result
  171.       end;
  172.  
  173.    medium_line_size: INTEGER is 80;
  174.  
  175. end -- PARSER_BUFFER
  176.  
  177.